package org.csu.mypetstore.controller;

import org.csu.mypetstore.domain.*;
import org.csu.mypetstore.service.AccountService;
import org.csu.mypetstore.service.OrderService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@MapperScan
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private Account account;
    @Autowired
    private List<Order> orderList;
    @Autowired
    private Cart cart;
    @Autowired
    private Order order;
    @Autowired
    private OrderService orderService;
    @Autowired
    private AccountService accountService;
    @Autowired
    private HttpSession session;

    @GetMapping("/view")
    public String view(Model model){
        account = (Account) session.getAttribute("account");
        model.addAttribute("account",account);
        return "catalog/main";
    }

    @GetMapping("/viewListOrder")
    public String viewListOrders(Model model){
        account = (Account) session.getAttribute("account");
        orderList = orderService.getOrdersByUsername(account.getUsername());
        session.setAttribute("orderList",orderList);
        model.addAttribute("account",account);
//        model.addAttribute("orderList",orderList);
        //跳到新
        return "order/ListOrders";
    }

    //生成订单
    @RequestMapping("/NewOrderForm")
    public String viewNewOrderForm(HttpSession session) {

        account = (Account) session.getAttribute("account");
        cart = (Cart) session.getAttribute("cart");

        if (account == null) {
            session.setAttribute("message", "You must sign on before attempting to check out.  Please sign on and try checking out again.");
            return "account/signOnForm";
        } else if (cart != null) {

            order.initOrder(account, cart);
            session.setAttribute("order", order);

            account = (Account) session.getAttribute("account");

            if (account != null) {

            }

            return "order/NewOrderForm";
        } else {
            session.setAttribute("message", "An order could not be created because a cart could not be found.");

            account = (Account) session.getAttribute("account");

            if (account != null) {

            }

            return "commom/error";
        }
    }

    //确认订单
    @RequestMapping("/ConfirmOrder")
    public String viewConfirmOrder(String shippingAddressRequired, HttpSession session) {
        String shippingAddressRequired1 = shippingAddressRequired;
        order = (Order) session.getAttribute("order");
        account = (Account) session.getAttribute("account");

        if (shippingAddressRequired1 == null) {
            if (account != null) {

            }

            return "order/ConfirmOrder";
        } else {
            shippingAddressRequired1 = null;

            if (account != null) {

            }

            return "order/ShippingOrder";
        }

    }

    @RequestMapping("/ShippingAddress")
    public String viewShippingAddress(String shipToFirstName, String shipToLastName, String shipAddress1, String shipAddress2, String shipCity, String shipState, String shipZip, String shipCountry, HttpSession session) {


        order = (Order) session.getAttribute("order");

        order.setShipToFirstName(shipToFirstName);
        order.setShipToLastName(shipToLastName);
        order.setShipAddress1(shipAddress1);
        order.setShipAddress2(shipAddress2);
        order.setShipCity(shipCity);
        order.setShipState(shipState);
        order.setShipZip(shipZip);
        order.setCourier(shipCountry);

        session.setAttribute("order", order);

        account = (Account) session.getAttribute("account");

        if (account != null) {

        }

        return "order/ConfirmOrder";
    }

    //最终查看订单
    @RequestMapping("/ViewOrder")
    public String viewOrder(HttpSession session) {

        order = (Order) session.getAttribute("order");
        cart = (Cart) session.getAttribute("cart");

        if (order != null) {

            orderService.insertOrder(order);
            session.setAttribute("order", order);
            //清空购物车
            cart = null;
            session.setAttribute("cart", cart);

            session.setAttribute("message", "Thank you, your order has been submitted.");

            //HttpSession session = request.getSession();
            Account account = (Account) session.getAttribute("account");

            if (account != null) {

            }

            return "order/ViewOrder";
        } else {
            session.setAttribute("message", "An error occurred processing your order (order was null).");
            return "common/error";
        }
    }
}

